const { ccclass, property } = cc._decorator;

@ccclass
export default class Test extends cc.Component {

    private ad: RewardedVideoAd = null;

    onLoad() {
        this.ad = zz.createRewardedVideoAd({ adUnitId: 'test' });
        if (null != this.ad) {
            this.ad.onError((res: { errCode: number, errMsg: string }) => {
                console.log(`激励错误,errCode=${res.errCode},errMsg=${res.errMsg}`);
            });
            this.ad.onClose((res: { isEnded: boolean }) => {
                console.log(`激励关闭，奖励=${res.isEnded}`);
            });
        }
    }

    onAdLoad() {
        if (null == this.ad) {
            return;
        }
        this.ad.load().then(() => {
            console.log('加载成功');
        }).catch(() => {
            console.log('加载失败');
        });
    }

    onAdShow() {
        if (null == this.ad) {
            return;
        }
        this.ad.load().then(() => {
            console.log('加载成功');
            this.ad.show().then(() => {
                console.log('显示成功');
            }).catch(() => {
                console.log('显示失败');
            });
        }).catch(() => {
            console.log('加载失败');
        });
    }
}
